package com.learning.algorithm.sort;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @Author: lishi
 * @Description: ${description}
 * @Date: 2021/11/19 11:52
 */
public class ArraysUtil {
    public static void swap(int[] arrays, int pos1, int pos2) {
        int i = arrays[pos1];
        arrays[pos1] = arrays[pos2];
        arrays[pos2] = i;
    }

    public static void insert(int[] arrays, int insertPos, int valuePos) {
        for (int i = insertPos; i < valuePos; i++) {
            swap(arrays, i, valuePos);
        }
    }

    public static int[] genNoDuplicateIntArray(int length) {
        int[] originArrays = new int[length];
        Set<Integer> integerSet = new HashSet<Integer>(length);
        for (int i = 0; i < length; i++) {
            while (true) {
                int num = new Random().nextInt(length * 10);
                if (integerSet.add(num)) {
                    originArrays[i] = num;
                    break;
                }
            }
        }
        return originArrays;
    }

    public static int[] genIntArray(int length) {
        int[] originArrays = new int[length];
        for (int i = 0; i < length; i++) {
            int num = new Random().nextInt(length * 10);
            originArrays[i] = num;
        }
        return originArrays;
    }
}
